
import { globalUtils } from '../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import uiData from 'ui-data';
import { TableSelectionTabs } from '../../common/js/table-selection-tabs';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewScope('menu-management', function (scope) {
  var jqAssignFunctionDialog;
  var jqTable1;
  var jqTable2;
  var jqSearchFunctionBtn;
  var tableSelectionTabs;
  var dataModel;

  scope.showAssignFunctionDialog = function (view, menuId) {
    if (globalUtils.isNullOrUndefined(jqAssignFunctionDialog)) {
      jqAssignFunctionDialog = view.$ui('assignFunctionDialog');
      jqTable1 = view.$ui('assignedFunctionTable');
      jqTable2 = view.$ui('assignableFunctionTable');
      jqSearchFunctionBtn = view.$ui('searchFunctionBtn');
      dataModel = view.getDataModel();

      tableSelectionTabs = new TableSelectionTabs({
        nav1: view.$ui('assignedFunctionNavTab'),
        tabPane1: view.$ui('assignedFunctionTabPane'),
        table1: jqTable1,
        table1Url: 'api/menu/menu/listAssignedFunctions',
        table1MoveUpDown: true,
        nav2: view.$ui('assignableFunctionNavTab'),
        tabPane2: view.$ui('assignableFunctionTabPane'),
        table2: jqTable2,
        table2Url: 'api/security/function/pageFunctionsByCond'
      }, [
        {
          field: 'id'
        },
        {
          field: 'functionCode'
        },
        {
          field: 'functionName'
        },
        {
          field: 'remark'
        }
      ], {
        confirmLabel: '${t("ui.system.common.operation.assign")}',
        cancelLabel: '${t("ui.system.common.operation.cancelAssign")}'
      });

      // 处理对话框事件
      addDialogEvent(view);
    }

    // 显示弹出框
    uiUtils.dialog.showModal(jqAssignFunctionDialog, view, {
      backdropParent: view.getViewElement(),
      //lockViewConfirmLeave: true,
      contextData: {
        menuId: menuId
      },
      onModalShow: function () {
        tableSelectionTabs.initOrResetTabs({
          id: menuId
        });
      }
    });
  };

  // 添加对话框事件
  function addDialogEvent(view) {
    jqAssignFunctionDialog.on(uiUtils.event.MODAL_HIDE, function () {
      uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
    });

    jqTable1.on(uiUtils.event.DATA_GRID_LOAD_SUCCESS, function () {
      uiUtils.view.checkFormDataChangeWhenLeave(scope, getDialogData);
    });

    jqSearchFunctionBtn.on(uiUtils.event.CLICK, function () {
      var data = dataModel.getData('searchFunctionField.*');
      data = uiData.utils.unprefix(data, 'searchFunctionField.');

      uiUtils.dataGrid.newQuery(jqTable2, {
        customQuery: data
      });
    });

    // 点击保存按钮的事件处理
    jqAssignFunctionDialog.on(uiUtils.event.CLICK, uiUtils.dialog.BTN_CONFIRM, function () {
      var contextData = uiUtils.dialog.getContextData(jqAssignFunctionDialog);
      var menuId = contextData.menuId;
      var functionIds = uiUtils.dataGrid.getFieldData(jqTable1, 'id');
      var data = {
        menuId: menuId,
        functionIds: functionIds
      };

      view.callService('api/menu/menu/assignFunctions', data)
        .waitResult(function (webResult) {
          if (uiUtils.common.checkWebResultSuccess(webResult)) {
            uiUtils.dialog.hideModal(jqAssignFunctionDialog);
            scope.refreshTreeMenuStructure();
            Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
          }
        });
    });
  }

  // 返回对话框的数据
  function getDialogData() {
    return dataModel.getData('assignedFunctions');
  }

});

/* SOURCE-CODE-END */


